<?php

/**
 * Framework_Module_Profile_CP
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Modules
 * @filesource
 */


/**
 * Framework_Module_Profile_CP
 *
 * @author Ian Szewczyk <roamzero@gmail.com>
 * @package Modules
 */
class Framework_Module_Profile_CP extends Framework_Module_CP
{

	/**
     * $navigators
     *
     * @access      public
     * @var         array		$navigators      Collection of Framework_Navigator objects
     */
	public $navigators;

	/**
     * $navParts
     *
     * @access      public
     * @var         array		$navParts      Url parts
     */
	public $navParts;

	/**
     * __construct
     *
     * Initializes the module: sets the default presenter, the allowed
     * conrollers, and the allowed events
     *
     * @access 		public
     * @return 		void
     */
	public function __construct()
	{
		parent::__construct(false);
		$this->tplFile = 'CpProfile.tpl';
		$this->navParts = Framework::$request->requestParts;
		// Build navigation
		$this->navigators['cpProfile'] = new Framework_Navigator('/'.$this->owner->loginName.'/Profile/CP/','cpProfileNavList');
		$this->navigators['cpProfile']->setItem('personalDetails','personal', 'Personal Details');
		$this->navigators['cpProfile']->setItem('contactInfo','contact', 'Contact Info');
		$this->navigators['cpProfile']->setItem('myInterests','interest', 'My Interests');
		
		
	}

	/**
     * __default
     *
     * Generates profile form(s)
     *
     * @access 		public
     * @return 		mixed 		boolean true on success, PEAR_Error on failure
     */
	public function __default()
	{
		// switch to choose form to build
		switch(true){


			case (strcasecmp($this->navParts[0], 'contact') == 0) :
				$table = 'ContactInfo';
				$this->navigators['cpProfile']->setActive('contactInfo');
				break;
				 
			case (strcasecmp($this->navParts[0], 'interest') == 0) :
				$table = 'InterestsInfo';
				$this->navigators['cpProfile']->setActive('myInterests');
				break;
				 
				 
			default:
				$table = 'PersonalInfo';
				$this->navigators['cpProfile']->setActive('personalDetails');
				break;
				 
		}

		// Set navigation
		$this->setData('cpProfileNav',$this->navigators['cpProfile']->getNavigation());
			
		return $this->buildForm($table);

	}

	/**
     * buildForm
     *
     * Build the form according to the table
     *
     * @access 		public
     * @param		string		$table		Table to generate form form
     * @return 		void
     */
	public function buildForm($table)
	{

		$this->table = $this->owner->getTable($table);
		
	    $this->forms['form'] = Framework_Quickform::factory('form', 'post', Framework::$request->getHTTPPath(), null, null, true);



		$col = $this->table->getColumns();

		$group = array();
		
		$formFields = array();

		foreach ($col AS $key => $val)
		{
			if($val['qf_label']) {

				$group[] = $this->table->getFormElement($key, 'value');
				$group[] = HTML_QuickForm::createElement('select', 'valuePrivacy', 'Privacy: ', array('1'=>'Me only','2'=>'Friends only','3'=>'Everyone'), $baseProperties);
				$group[1]->setValue(3);
				$this->forms['form']->addGroup($group, $key, $group[0]->getLabel());
				$formFields[] = $key;
				$group = null;

			}


		}
		
		$this->setData('formFields', $formFields);
		
		// Submit button
		$this->forms['form']->addElement('submit','btnSubmit','Update','class="button" id="updateButton"');
		// Filters
		$this->forms['form']->applyFilter('__ALL__','trim');
		$this->forms['form']->applyFilter('__ALL__',array(Framework::$util,'filter_removeXSS'));


		if ($this->forms['form']->validate()) {
			$result = $this->forms['form']->process(array($this, 'process'),true);
			
		} else {
			$this->fillForm();
		}
		
		
			
	}

	/**
     * process
     *
     *
     * @access 		public
     * @param 		array		$data		Data to process
     * @return 		void
     */
	public function process($data)
	{


		$publicBucket = $this->table->getBlankRow();

		$friendBucket = $this->table->getBlankRow();

		$privateBucket = $this->table->getBlankRow();

		foreach($data as $key => $value){
			switch ($value['valuePrivacy']) {
				case 1:
					$privateBucket[$key] = $value['value'];
					break;

				case 2:
					$friendBucket[$key] = $value['value'];
					break;

				case 3:
					$publicBucket[$key] = $value['value'];
					break;
			}
		}


		$publicBucket['idOwner'] = $this->owner->idOwner;
		$publicBucket['privacy'] = 3;

		$friendBucket['idOwner'] = $this->owner->idOwner;
		$friendBucket['privacy'] = 2;

		$privateBucket['idOwner'] = $this->owner->idOwner;
		$privateBucket['privacy'] = 1;

		$this->save($publicBucket);
		$this->save($friendBucket);
		$this->save($privateBucket);
		
	

		$this->fillForm();
		
		$this->setData('formResponse', 'Profile Updated');


	}

	/**
     * process
     *
     *
     * @access 		public
     * @param 		array		$bucket		Record to insert
     * @return 		void
     */
	private function save($bucket)
	{
		$where = array('idOwner' => $this->owner->idOwner, 'privacy' => $bucket['privacy']);
		$this->table->load($where);
		$this->table->record = $bucket;
		$result = $this->table->save();
		$this->table->resetRecord();
		return $result;

	}

	/**
     * fillForm
     *
     *
     * @access 		public
     * @return 		void
     */
	public function fillForm()
	{
		$queryArray = array(
			'sqlKey' => array(
				'select' => '*',
				'where'  => 'idOwner = '.$this->owner->idOwner.' AND privacy IN(1,2,3)'
			)
		);

		// Raw
		$details = $this->table->select($queryArray);

		// Processed
		$profileDetail = array();

		foreach($details AS $detail) {
				
			foreach($detail AS $key => $val) {
				if ($key != 'idOwner' && $key != 'privacy' && $val != null) {
					$profileDetail[$key] = array('value' => $val, 'valuePrivacy' => $detail['privacy']);
				}
				
				if ($profileDetail != null) {
					$this->forms['form']->setDefaults($profileDetail);
				}

				$profileDetail = null;
			}
		}
	}




}

?>